<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/2/20
 * Time: 8:36
 */
namespace app\admin\controller;


use think\Db;
use think\Request;
class About extends Base {
    /**
     * 意见反馈
     * @return mixed|\think\response\Json
     */
    public function feedback(){
        if(request()->isAjax ()){
            $where = [];
            $keyword = input('keyword');
            $utype = input('ct_stats');
            if(isset($keyword) && $keyword != ''){
                $where['feed_content'] = ['like', '%' . $keyword . '%'];
            }
            if(isset($utype) && $utype != "")
            {
                $where['feed_status'] = $utype;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_feedback')->alias('f')
                ->join('up_u_user u','f.feed_uid=u.user_id','LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_feedback')->alias('f')
                ->join('up_u_user u','f.feed_uid=u.user_id','LEFT')
                ->where($where)
                ->order('feed_status asc,feed_time desc')
                ->page($Nowpage,$limits)
                ->field('f.*,user_username,user_phone')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch('about/feedback');
    }
    /**
     * 处理意见反馈
     * @return \think\response\Json
     */
    public function feedbackstatus(){
        $user_id = input('id');
        $res = Db::table('up_feedback')->where('feed_id', $user_id)->setField('feed_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    /**
     * 删除意见反馈
     * @return \think\response\Json
     */
    public function feedbackdel(){
        $user_id = input('id');
        $res = Db::table('up_feedback')->where('feed_id', $user_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    /**
     * 编辑意见反馈备注
     * @return mixed|\think\response\Json
     */
    public function feedbackEdit(){
        if(Request::instance()->isPost()){
            $feed_id = input('feed_id');
            $feed_remark = trim(input('feed_remark'), ' ');
            $data = [
                'feed_remark' => $feed_remark,
                'feed_update' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_feedback')
                ->where('feed_id', $feed_id)
                ->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $feed_id = input('feed_id');
        // banner
        $info = Db::table('up_feedback')
            ->where('feed_id', $feed_id)
            ->find();
        $this->assign([
            'feed_id' => $feed_id,
            'info' => $info
        ]);
        return $this->fetch('about/feedbackedit');
    }
    /**
     * 常见问题
     * @return mixed|\think\response\Json
     */
    public function question(){
        if(request()->isAjax ()){
            $where = [];
            $keyword = input('keyword');
            $utype = input('ct_stats');
            if(isset($keyword) && $keyword != ''){
                $where['q_title'] = ['like', '%' . $keyword . '%'];
            }
            if(isset($utype) && $utype != "")
            {
                $where['q_status'] = $utype;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_question')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_question')
                ->where($where)
                ->order('q_status asc,q_time desc')
                ->page($Nowpage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    /**
     * 常见问题-启用
     * @return \think\response\Json
     */
    public function questionyes(){
        $user_id = input('id');
        $res = Db::table('up_question')->where('q_id', $user_id)->setField('q_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    /**
     * 常见问题-禁用
     * @return \think\response\Json
     */
    public function questionno(){
        $user_id = input('id');
        $res = Db::table('up_question')->where('q_id', $user_id)->setField('q_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    /**
     * 常见问题-删除
     * @return \think\response\Json
     */
    public function questiondel(){
        $user_id = input('id');
        $res = Db::table('up_question')->where('q_id', $user_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    /**
     * 添加问题
     * @return mixed|\think\response\Json
     */
    public function questionAdd(){
        if(Request::instance()->isPost()){
            $q_id = input('q_id');
            $q_title = trim(input('q_title'), ' ');
            $q_status = input('q_status');
            $q_content = input('q_content');
            $data = [
                'q_title' => $q_title,
                'q_status' => $q_status,
                'q_content' => $q_content,
                'q_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_question')
                ->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        return $this->fetch('about/questionadd');
    }
    /**
     * 编辑问题
     * @return mixed|\think\response\Json
     */
    public function questionEdit(){
        if(Request::instance()->isPost()){
            $q_id = input('q_id');
            $q_title = trim(input('q_title'), ' ');
            $q_status = input('q_status');
            $q_content = input('q_content');
            $data = [
                'q_title' => $q_title,
                'q_status' => $q_status,
                'q_content' => $q_content,
                'q_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_question')
                ->where('q_id', $q_id)
                ->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '操作成功']);
            }else{
                return json(['code' => 100, 'msg' => '操作失败']);
            }
        }
        $q_id = input('q_id');
        // banner
        $info = Db::table('up_question')
            ->where('q_id', $q_id)
            ->find();
        $this->assign([
            'q_id' => $q_id,
            'info' => $info
        ]);
        return $this->fetch('about/questionedit');
    }

    /**
     * 关于我们
     * @return mixed|\think\response\Json
     */
    public function about(){
        if(Request::instance()->isPost()){
            $user = input('user');
            $yinsi = input('yinsi');
            $aboutus = input('aboutus');
            $phone = input('phone');
            $res = Db::table('up_about')->where('id', 1)->update(['content' => $aboutus]);
            $res = Db::table('up_about')->where('id', 2)->update(['content' => $phone]);
            $res = Db::table('up_about')->where('id', 3)->update(['content' => $user]);
            $res = Db::table('up_about')->where('id', 4)->update(['content' => $yinsi]);
            if($res){
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        $about_us = Db::table('up_about')->where('id', 1)->find();
        $phone = Db::table('up_about')->where('id', 2)->find();
        $user = Db::table('up_about')->where('id', 3)->find();
        $yinsi = Db::table('up_about')->where('id', 4)->find();
        $this->assign('about_us', $about_us);
        $this->assign('phone', $phone);
        $this->assign('user', $user);
        $this->assign('yinsi', $yinsi);
        return $this->fetch('about/about');
    }

    /**
     * 联系我们
     * @return mixed|\think\response\Json
     */
    public function phone(){
        if(Request::instance()->isPost()){
            $about_content = input('about_content');
            $res = Db::table('up_about')->where('id', 2)->update(['phone' => $about_content]);
            if($res){
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        $about_content = Db::table('up_about')->where('id', 2)->value('phone');
        $this->assign('content', $about_content);
        return $this->fetch('about/phone');
    }

    /**
     * 隐私协议
     * @return mixed|\think\response\Json
     */

    public function yinsi(){
        if(Request::instance()->isPost()){
            $about_content = input('about_content');
            $zhuce_content = input('zhuce_content');
            $res = Db::table('up_about')->where('id', 3)->update(['content' => $about_content]);
            $res = Db::table('up_about')->where('id', 7)->update(['content' => $zhuce_content]);
            if($res){
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        $about_content = Db::table('up_about')->where('id', 3)->value('content');
        $zhuce_content = Db::table('up_about')->where('id', 7)->value('content');
        $this->assign('content', $about_content);
        $this->assign('content2', $zhuce_content);
        return $this->fetch('about/yinsi');
    }

    /**
     * 派逊天地
     * @return mixed|\think\response\Json
     */

    public function world(){
        if(Request::instance()->isPost()){
            $about_content = input('about_content');
            $res = Db::table('up_about')->where('id', 4)->update(['content' => $about_content]);
            if($res){
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        $about_content = Db::table('up_about')->where('id', 4)->value('content');
        $this->assign('content', $about_content);
        return $this->fetch('about/world');
    }


    /**
     * 平台退货地址
     * @return mixed|\think\response\Json
     */
    public function address(){
        if(Request::instance()->isPost()){
            $about_content = input('about_content');
            $phone = input('phone');
            $res = Db::table('up_about')->where('id', 5)->update(['content' => $about_content,'phone' => $phone]);
            if($res){
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        $about_content = Db::table('up_about')->where('id',5)->find();
        $this->assign('content', $about_content);
        return $this->fetch('about/address');
    }
    /**
     * 尺寸图
     * @return mixed|\think\response\Json
     */
    public function sizepic(){
        if(Request::instance()->isPost()){
            $about_content = input('content');
            $res = Db::table('up_about')->where('id', 6)->update(['content' => $about_content]);
            if($res){
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        $about_content = Db::table('up_about')->where('id',6)->value('content');
        $this->assign('content', $about_content);
        return $this->fetch('about/sizepic');
    }
    /**
     * 运费配置
     * @return mixed|\think\response\Json
     */
    public function postman(){
        if(Request::instance()->isPost()){
            $default_num = input('default_num');
            $default_price = input('default_price');
            $over_price = input('over_price');
            $res = Db::table('up_postman_config')->where('id',1)->update([
                'default_num' => $default_num,
                'default_price' => $default_price,
                'over_price' => $over_price,
            ]);
            if($res){
                return json(['code' => 200, 'msg' => '修改成功']);
            }else{
                return json(['code' => 100, 'msg' => '修改失败或者未做任何修改']);
            }
        }
        $data = Db::table('up_postman_config')->where('id',1)->find();
        $this->assign('data', $data);
        return $this->fetch('about/postman');
    }
    /**
     * 自营商城是否开启
     * @return mixed|\think\response\Json
     */
    public function shopCan(){
        if(Request::instance()->isPost()){
            $status = input('status');
            $phone = input('phone');
            $res = Db::table('up_about')->where('id',8)->update([
                'content' => $status,
                'phone' => $phone
            ]);
            if($res){
                return json(['code' => 200, 'msg' => '修改成功']);
            }else{
                return json(['code' => 100, 'msg' => '修改失败或者未做任何修改']);
            }
        }
        $data = Db::table('up_about')->where('id',8)->find();
        $this->assign('data', $data);
        return $this->fetch('about/shopcan');
    }
    /**
     * 购买后几小时推送给erp
     * @return mixed|\think\response\Json
     */
    public function changesize(){
        if(Request::instance()->isPost()){
            $status = input('content');
            $status = (int)$status;
            if($status < 1){
                return json(['code' => 100, 'msg' => '不能小于1']);
            }
            $res = Db::table('up_about')->where('id',9)->update([
                'content' => $status
            ]);
            if($res){
                return json(['code' => 200, 'msg' => '修改成功']);
            }else{
                return json(['code' => 100, 'msg' => '修改失败或者未做任何修改']);
            }
        }
        $data = Db::table('up_about')->where('id',9)->find();
        $this->assign('data', $data);
        return $this->fetch('about/changesize');
    }
    /**
     * 购买后几小时推送给erp
     * @return mixed|\think\response\Json
     */
    public function donedays(){
        if(Request::instance()->isPost()){
            $status = input('content');
            $status = (int)$status;
            if($status < 1){
                return json(['code' => 100, 'msg' => '不能小于1']);
            }
            $res = Db::table('up_about')->where('id',10)->update([
                'content' => $status
            ]);
            if($res){
                return json(['code' => 200, 'msg' => '修改成功']);
            }else{
                return json(['code' => 100, 'msg' => '修改失败或者未做任何修改']);
            }
        }
        $data = Db::table('up_about')->where('id',10)->find();
        $this->assign('data', $data);
        return $this->fetch('about/donedays');
    }
}